<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class PasswordHelper {

	function crypt_password($password) {
		if ($password) {
			//find the longest valid salt allowed by server
			$max_salt = CRYPT_SALT_LENGTH;

			//blowfish hashing with a salt as follows: "$2a$", a two digit cost parameter, "$", and 22 base 64
			$sha512 = '$6$rounds=5000$';

			//get the longest salt, could set to 22 crypt ignores extra data
			$salt = get_salt ( $max_salt );

			//get a second salt to strengthen password
			$salt2 = get_salt ( 30 ); //set to whatever


			//append salt2 data to the password, and crypt using salt, results in a 60 char output
			$crypt_pass = crypt ( $password . $salt2, $sha512 . $salt );

			//insert crypt pass along with salt2 into database.
			$sql = "insert into database....";

			echo "Salt1: " . $salt . "    " . "<br /> Salt2: " . $salt2 . "<br />";
			echo "Hashed: " . $crypt_pass . "<br />";
			
			return true;
		}
	}  


	function get_salt($length) {
		$options = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';

		$salt = '';

		for($i = 0; $i <= $length; $i ++) {
			$options = str_shuffle ( $options );
			$salt .= $options [mt_rand ( 0, 63 )];
		}
		return $salt;
	}

	function verify_password($input_password, $stored_password, $stored_salt)
	{
		if($input_password)
		{
			//compare the crypt of input+stored_salt to the stored crypt password
			if (crypt($input_password . $stored_salt, $stored_password) == $stored_password) {
				//authenticated
				return true;
			}
			else return false;
		}
		else return false;
	}
}

/* End of file PasswordHelper.php */
?>